package cn.cxj.quartz.jobs;

import cn.cxj.order.domain.Product;
import cn.cxj.order.domain.ProductOrder;
import cn.cxj.order.mapper.ProductOrderMapper;
import cn.cxj.order.service.IProductOrderService;
import cn.cxj.order.service.IProductService;
import cn.cxj.pay.domain.PayBill;
import cn.cxj.pay.service.IPayBillService;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.Map;

public class ProductOrderJob implements Job {

    @Autowired
    private IPayBillService payBillService;

    @Autowired
    private IProductOrderService productOrderService;

    @Autowired
    private IProductService productService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        JobDataMap jobDataMap = jobExecutionContext.getJobDetail().getJobDataMap();
        Map<String, Object> params = (Map<String, Object>) jobDataMap.get("params");
        String orderSn = (String) params.get("orderSn");
        // 根据订单号查询t_pay_bill表,修改状态为未支付和更新修改时间
        PayBill payBill = payBillService.queryByOrderSn(orderSn);
        payBill.setState(-1);
        payBill.setUpdateTime(new Date());
        payBillService.update(payBill);
        // 修改服务订单的状态
        Long businessKey = payBill.getBusinessKey();
        ProductOrder productOrder = productOrderService.queryById(businessKey);
        productOrder.setState(-1);
        productOrderService.update(productOrder);
        // 根据product_id查询product,销量减一
        Product product = productService.queryById(productOrder.getProduct_id());
        product.setSalecount(product.getSalecount() - 1);
        productService.update(product);

    }

}
